﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace Updater
{
    class Program
    {
        public const string AppName = "EzMemo.exe";

        static void Main(string[] args)
        {
            try
            {
                Thread.Sleep(3000);

                KillApplication();

                Thread.Sleep(2000);

                string sourceFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                sourceFilePath = Path.Combine(sourceFilePath, "Installer");

                if (Directory.Exists(sourceFilePath))
                {
                    DirectoryInfo folderInfo = new DirectoryInfo(sourceFilePath);
                    FileInfo[] files = folderInfo.GetFiles();

                    foreach (FileInfo f in files)
                    {
                        File.Copy(f.FullName, Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), f.Name), true);
                    }

                    try
                    {
                        Directory.Delete(sourceFilePath, true);
                    }
                    catch
                    {
                        DeleteDirectory(sourceFilePath);
                    }
                }

                StartApplication();
            }
            catch (Exception ex)
            {
                Utility.ReportError("Updater.exe -> Main", ex);
            }
        }

        public static void StartApplication()
        {
            Process app = new Process(); ;

            try
            {
                string descFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), AppName );

                app.StartInfo.FileName = descFilePath;
                app.StartInfo.UseShellExecute = true;
                app.Start();
            }
            catch (Exception ex)
            {
                Utility.ReportError("Updater: StartApplication", ex);
            }
        }

        public static void KillApplication()
        {
            Process[] processes = Process.GetProcesses();

            foreach (Process pro in processes)
            {
                try
                {
                    if ((pro.Modules != null))
                    {

                        if (pro.Modules.Count > 0)
                        {

                            if (pro.ProcessName.ToLower() == Path.GetFileNameWithoutExtension(AppName).ToLower())
                            {
                                pro.Kill();
                                return;

                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }

            }

        }

        private static bool DeleteDirectory(string target_dir)
        {
            bool result = false;

            string[] files = Directory.GetFiles(target_dir);
            string[] dirs = Directory.GetDirectories(target_dir);

            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }

            Directory.Delete(target_dir, false);

            return result;
        }

    }

}
